package cn.dhnep.demo;

import cn.dhnep.demo.pojo.DeliveryStatistics;
import cn.dhnep.demo.pojo.RecyclingStatistics;
import cn.dhnep.demo.pojo.frontvo.*;
import cn.dhnep.demo.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Component
@Slf4j
public class StatisticsTask {

    @Resource
    private RecyclingStatisticsService recyclingStatisticsService;
    @Resource
    private UserService userService;
    @Resource
    private OrderService orderService;

    @Resource
    private DeliveryStatisticsService deliveryStatisticsService;
    @Resource
    private PackageOrderItemService packageOrderItemService;

    /**
     * 每天23点骑手回收数据和推广数据定时任务
     */
    @Scheduled(cron = "0 0 23 * * ?")
    public void recyclingStatistics() {
        //骑手昨日的回收状态、重量、金额
        List<OrderVo> orderVos = orderService.horsemanEvenDayEarnings();
        List<String> nameList = orderVos.stream().map(OrderVo::getHorsemanName).collect(Collectors.toList());

        //骑手昨日的推广用户数和累计推广用户数
        List<UserAddCount> yesterdayAddUserByHorseman = userService.getYesterdayAddByHorseman();
        for (UserAddCount userAddCount : yesterdayAddUserByHorseman) {
            userAddCount.setSumCount(userService.userCount(userAddCount.getUserPhone()));
            nameList.add(userAddCount.getUserName());
        }

        //将骑手名字全部放入list后去重
        List<String> collect = nameList.stream().distinct().collect(Collectors.toList());
        List<RecyclingStatistics> list = new ArrayList<>();

        for (String name : collect) {
            if (!StringUtils.isEmpty(name)) {
                RecyclingStatistics statistics = new RecyclingStatistics().setHorsemanName(name)
                        .setStatisticsTime(new Date(new Date().getTime() - 86400000L)); //昨天的日期
                list.add(statistics);

                yesterdayAddUserByHorseman.forEach(u -> {
                    if (name.equals(u.getUserName()))
                        //设置骑手的今日推广和累计推广
                        statistics.setAddUser(u.getAddCount()).setAllUser(u.getSumCount());
                });

                orderVos.forEach(orderVo -> {
                    if (name.equals(orderVo.getHorsemanName())) {
                        //设置回收总重量、总金额、当天订单数
                        statistics.setAllWeight(orderVo.getPayOrderMoney()).setAllMoney(orderVo.getPayPayMoney())
                                .setOrderCount(orderVo.getOrderCount());

                        //设置订单状态量
                        for (OrderAddCount orderAddCount : orderVo.getOrderAddCounts()) {
                            if (orderAddCount.getOrderState().equals("已完成"))
                                statistics.setCompletedOrder(orderAddCount.getOrderCount());
                            if (orderAddCount.getOrderState().equals("进行中"))
                                statistics.setIncompletedOrder(orderAddCount.getOrderCount());
                        }

                        //设置回收类型重量、金额
                        for (OrderItemVo orderItemVo : orderVo.getOrderItemVos()) {
                            switch (orderItemVo.getProductType()) {
                                case "塑料类":
                                    statistics.setPlastic(orderItemVo.getFinallyWeight()).setPlasticMoney(orderItemVo.getSum());
                                    break;
                                case "废纸类":
                                    statistics.setPaper(orderItemVo.getFinallyWeight()).setPaperMoney(orderItemVo.getSum());
                                    break;
                                case "纺织物":
                                    statistics.setTextiles(orderItemVo.getFinallyWeight()).setTextilesMoney(orderItemVo.getSum());
                                    break;
                                case "金属类":
                                    statistics.setMetal(orderItemVo.getFinallyWeight()).setMetalMoney(orderItemVo.getSum());
                                    break;
                                case "家电类":
                                    statistics.setElectrical(orderItemVo.getAmount()).setElectricalMoney(orderItemVo.getSum());
                                    break;
                                case "大件废弃物":
                                    statistics.setWaste(orderItemVo.getAmount()).setWasteMoney(orderItemVo.getSum());
                                    break;
                            }
                        }
                    }
                });
            }
        }

        log.debug(recyclingStatisticsService.saveBatch(list) ? "统计回收数据成功！" : "统计回收数据失败！");
    }

    /**
     * 每天24点骑手交货数据统计
     */
    @Scheduled(cron = "0 0 0 */1 * ?")
    public void deliveryStatistics() {
        //骑手交货订单总数、已完成数、交货总重量、交货总金额
        List<PackageOrderItemVo> packageOrderItemVos = packageOrderItemService.horsemanEvenDayPackageOrder();
        List<DeliveryStatistics> list = new ArrayList<>();

        for (PackageOrderItemVo packageOrderItemVo : packageOrderItemVos) {
            DeliveryStatistics deliveryStatistics = new DeliveryStatistics();
            list.add(deliveryStatistics);

            deliveryStatistics.setStatisticsTime(new Date(new Date().getTime() - 86400000L)) //昨天的日期
                    .setHorsemanName(packageOrderItemVo.getHorseManName())
                    .setOrderCount(packageOrderItemVo.getAllCount())
                    .setCompletedOrder(packageOrderItemVo.getCompletedCount())
                    .setAllWeight(packageOrderItemVo.getRecycleWeigh())
                    .setAllMoney(packageOrderItemVo.getSumRecycle());

            for (OrderItemVo orderItemVo : packageOrderItemVo.getOrderItemVos()) {
                switch (orderItemVo.getProductType()) {
                    case "塑料":
                        deliveryStatistics.setPlastic(orderItemVo.getFinallyWeight()).setPlasticMoney(orderItemVo.getSum());
                        break;
                    case "纸类":
                        deliveryStatistics.setPaper(orderItemVo.getFinallyWeight()).setPaperMoney(orderItemVo.getSum());
                        break;
                    case "纺织物":
                        deliveryStatistics.setTextiles(orderItemVo.getFinallyWeight()).setTextilesMoney(orderItemVo.getSum());
                        break;
                    case "金属类":
                        deliveryStatistics.setMetal(orderItemVo.getFinallyWeight()).setMetalMoney(orderItemVo.getSum());
                        break;
                    case "电子废弃类":
                        deliveryStatistics.setElectrical(orderItemVo.getFinallyWeight()).setElectricalMoney(orderItemVo.getSum());
                        break;
                    case "大件废弃物": //交货品类表中并没有 大件废弃物 这个类别
                        deliveryStatistics.setWaste(orderItemVo.getAmount()).setWasteMoney(orderItemVo.getSum());
                        break;
                }
            }
        }

        log.debug(deliveryStatisticsService.saveBatch(list) ? "统计交货数据成功！" : "统计交货数据失败！");
    }
}
